﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace ScoutNET.Attributes
{
    public class AuthorizeView : AuthorizeAttribute
    {
        #region Properties
        public String ErrorMessageKey { get; set; }
        public bool ShowToCrawler { get; set; }
        #endregion

        #region Methodes
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (httpContext == null) throw new ArgumentNullException("httpContext");

            //Make sure the user is authenticated.
            if (httpContext.User.Identity.IsAuthenticated == false)
            {
                if (httpContext.Request.Browser.Crawler == false && !httpContext.Request.Headers["User-Agent"].ToLower().Contains("facebook"))
                {
                    return false;
                }
                else
                {
                    return ShowToCrawler;
                }
            }
            else
            {
                return true;
            }

        }

        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);
            if (filterContext.Result is HttpUnauthorizedResult && ErrorMessageKey != null && ErrorMessageKey != string.Empty)
            {
                ViewDataDictionary viewData = new ViewDataDictionary();

                string errorMessage = Resources.ResourceManager.GetString(ErrorMessageKey);
                viewData.Add("ErrorMessage", errorMessage.ToString());
                
                string[] saBreadcrumb = new String[2];
                saBreadcrumb[0] = HtmlHelper.GenerateLink(filterContext.RequestContext, RouteTable.Routes, Resources.Master_Breadcrumb_Home, "Default", "Index", "Home", null, null);
                saBreadcrumb[1] = Resources.Error_Error_Breadcrumb;

                viewData.Add("Breadcrumb", saBreadcrumb);
                viewData.Add("PageTitle", Resources.Error_LoginIn_Title);
                if (filterContext.RouteData.Values.ContainsKey("sectionName"))
                {
                    viewData.Add("SystemName", filterContext.RouteData.Values["sectionName"]);
                }

                viewData.Add("Master_RSS_Unite", Resources.Master_RSS_Unite);
                viewData.Add("Master_RSS_Scout", Resources.Master_RSS_Scout);
                viewData.Add("Master_RSS_Pionnier", Resources.Master_RSS_Pionnier);
                viewData.Add("Master_RSS_Nuton", Resources.Master_RSS_Nuton);
                viewData.Add("Master_RSS_Lutin", Resources.Master_RSS_Lutin);
                viewData.Add("Master_RSS_Louveteau", Resources.Master_RSS_Louveteau);
                viewData.Add("Master_RSS_Guide", Resources.Master_RSS_Guide);

                filterContext.Result = new ViewResult { ViewName = "ErrorAcces", ViewData = viewData };                  
            }
        }
        #endregion
    }
}
